package com.coconet.gas.rest;

import com.alibaba.fastjson.JSONObject;
import com.coconet.gas.biz.BaseBiz;
import com.coconet.gas.constant.dwEnum;
import com.coconet.gas.vo.dw.st.CommonFy;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import response.BaseResponse;
import response.ObjectRestResponse;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @param <T>
 * @create 2017-06-15 8:48
 */

public class BaseController<Biz extends BaseBiz, Entity> {
	@Autowired
	protected HttpServletRequest request;
	@Autowired
	protected Biz baseBiz;

	// @RequestMapping(value = "",method = RequestMethod.PUT)
	// @ResponseBody
	// public String add(Entity entity){
	// return JSONObject.toJSONString(baseBiz.insertSelective(entity));
	// }
	@ApiOperation(value = "根据ID查找", httpMethod = "GET")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String get(@PathVariable Object id) {

		Object o = baseBiz.selectById(id);

		return JSONObject.toJSONString(o);
	}

	 @RequestMapping(value = "/update", method = RequestMethod.PUT)
	 @ResponseBody
	 public BaseResponse update(@RequestBody Entity entity) {
 		 baseBiz.updateSelectiveById(entity);
	 return new BaseResponse(dwEnum.SUCCEESS.code(), "");
	 }
	 
	 @RequestMapping(value = "simpleAdd", method = RequestMethod.POST)
	 @ResponseBody
	 public ObjectRestResponse insert(@RequestBody Entity entity) {
		 baseBiz.insertSelective(entity);
	 return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "",entity);
	 }
	@ApiOperation(value = "根据ID删除", httpMethod = "DELETE")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public String remove(@PathVariable Object id) {
		return JSONObject.toJSONString(baseBiz.deleteById(id));
	}
	@ApiOperation(value = "批量ID删除", httpMethod = "POST")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public BaseResponse remove(@RequestBody List<String> idlist) {
		for (Object op : idlist) {
			baseBiz.deleteById(op);
		}
		return new BaseResponse(dwEnum.SUCCEESS.code(), "");
	}

	@RequestMapping(value = "/all", method = RequestMethod.GET)
	@ResponseBody
	@Cacheable(cacheNames = "member")
	@ApiOperation(value = "/all", notes = "得到所有")
	public String all() {
		return JSONObject.toJSONString(baseBiz.selectListAll());
	}

	@ApiOperation(value = "分页 搜索", httpMethod = "POST")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String listdecivema(@RequestBody @Valid CommonFy cfy) {

		Class clz;
		Object obj = null;
		try {
			clz = Class.forName("com.coconet.ifms.vo.dw." + cfy.getEntity());
			obj = clz.newInstance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String, Object> inMap = JSONObject.parseObject(cfy.getInStr());
		Map<String, Object> orMap = JSONObject.parseObject(cfy.getOrStr());
		Map<String, Object> eqMap = JSONObject.parseObject(cfy.getEqStr());
		Map<String, Object> map = JSONObject.parseObject(cfy.getMapStr(), HashMap.class);
		return baseBiz.fy(obj, request, cfy.getCurrentPage(), cfy.getPageSize(), cfy.getStartime(), cfy.getEndtime(), map,inMap,orMap, eqMap,cfy.getSort());
	}

	

}
